package com.spark.client.tool;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;

/**
 * 星火大模型工具类
 * 支持函数调用和联网搜索
 */
public class SparkTool {
    
    /**
     * 工具类型：function 或 web_search
     */
    private String type;
    
    /**
     * 函数定义（当 type 为 function 时使用）
     */
    private FunctionDefinition function;
    
    /**
     * 联网搜索配置（当 type 为 web_search 时使用）
     */
    @JsonProperty("web_search")
    private WebSearchConfig webSearch;

    public SparkTool() {}

    public SparkTool(String type) {
        this.type = type;
    }

    /**
     * 创建函数工具
     * @param name 函数名称
     * @param description 函数描述
     * @param parameters 参数定义（JSON Schema 格式）
     * @return SparkTool
     */
    public static SparkTool function(String name, String description, Map<String, Object> parameters) {
        SparkTool tool = new SparkTool("function");
        tool.function = new FunctionDefinition(name, description, parameters);
        return tool;
    }

    /**
     * 创建联网搜索工具
     * @param enable 是否启用
     * @param showRefLabel 是否显示引用标签
     * @param searchMode 搜索模式 (normal/deep)
     * @return SparkTool
     */
    public static SparkTool webSearch(boolean enable, boolean showRefLabel, String searchMode) {
        SparkTool tool = new SparkTool("web_search");
        tool.webSearch = new WebSearchConfig(enable, showRefLabel, searchMode);
        return tool;
    }

    /**
     * 创建默认联网搜索工具
     * @return SparkTool
     */
    public static SparkTool webSearch() {
        return webSearch(true, false, "normal");
    }

    /**
     * 函数定义
     */
    public static class FunctionDefinition {
        private String name;
        private String description;
        private Map<String, Object> parameters;

        public FunctionDefinition() {}

        public FunctionDefinition(String name, String description, Map<String, Object> parameters) {
            this.name = name;
            this.description = description;
            this.parameters = parameters;
        }

        // Getters and Setters
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public Map<String, Object> getParameters() {
            return parameters;
        }

        public void setParameters(Map<String, Object> parameters) {
            this.parameters = parameters;
        }
    }

    /**
     * 联网搜索配置
     */
    public static class WebSearchConfig {
        private Boolean enable;
        @JsonProperty("show_ref_label")
        private Boolean showRefLabel;
        @JsonProperty("search_mode")
        private String searchMode;

        public WebSearchConfig() {}

        public WebSearchConfig(Boolean enable, Boolean showRefLabel, String searchMode) {
            this.enable = enable;
            this.showRefLabel = showRefLabel;
            this.searchMode = searchMode;
        }

        // Getters and Setters
        public Boolean getEnable() {
            return enable;
        }

        public void setEnable(Boolean enable) {
            this.enable = enable;
        }

        public Boolean getShowRefLabel() {
            return showRefLabel;
        }

        public void setShowRefLabel(Boolean showRefLabel) {
            this.showRefLabel = showRefLabel;
        }

        public String getSearchMode() {
            return searchMode;
        }

        public void setSearchMode(String searchMode) {
            this.searchMode = searchMode;
        }
    }

    // Main class Getters and Setters
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public FunctionDefinition getFunction() {
        return function;
    }

    public void setFunction(FunctionDefinition function) {
        this.function = function;
    }

    public WebSearchConfig getWebSearch() {
        return webSearch;
    }

    public void setWebSearch(WebSearchConfig webSearch) {
        this.webSearch = webSearch;
    }

    @Override
    public String toString() {
        return "SparkTool{" +
                "type='" + type + '\'' +
                ", function=" + function +
                ", webSearch=" + webSearch +
                '}';
    }
}
